package com.icbc.open.chengdu.demo.account.query;

import com.csii.pp.icbc.util.Util;
import com.icbc.api.DefaultIcbcClient;
import com.icbc.api.internal.util.fastjson.annotation.JSONField;
import com.icbc.api.internal.util.internal.util.fastjson.JSON;
import com.icbc.api.request.SettlementAccountBindingQueryRequestV1;
import com.icbc.api.response.SettlementAccountBindingQueryResponseV1;
import com.icbc.open.chengdu.demo.BaseData;
import com.icbc.open.chengdu.demo.utils.GenerateKeyUtils;
import com.icbc.open.chengdu.demo.utils.SecretUtils;

/**
 * 绑定卡查询
 *
 * @author xcz
 */
public class SettlementAccountBindingQueryTest {

    public static void main(String[] args) throws Exception {
        String sm4Key = GenerateKeyUtils.generateSM4Key(); //生成SM4密钥
        DefaultIcbcClient client = new DefaultIcbcClient(BaseData.APP_ID, BaseData.MY_RSA_PRIVATE_KEY, BaseData.ICBC_RSA_PUBLIC_KEY);
        SettlementAccountBindingQueryRequestV1 request = new SettlementAccountBindingQueryRequestV1();
        SettlementAccountBindingQueryRequestV1.SettlementAccountBindingQueryRequestV1Biz bizContent = new SettlementAccountBindingQueryRequestV1.SettlementAccountBindingQueryRequestV1Biz();
        request.setServiceUrl(BaseData.ACCOUNT_BINDING_QUERY_URL);//url
        bizContent.setCorpNo(BaseData.APP_ID); //合作方机构编号
        bizContent.setTrxAccDate(BaseData.TEST_TRX_ACC_DATE); //合作方交易日期
        bizContent.setTrxAccTime(BaseData.TEST_TRX_ACC_TIME); //合作方交易时间
        bizContent.setCorpDate(BaseData.TEST_CORP_DATE); //合作方工作日期
        bizContent.setCorpSerno(Util.getNonceStr(16)); //合作方交易单号
        bizContent.setOutServiceCode("querybinding"); //外部服务代码
        bizContent.setMediumId(SecretUtils.encryptSM4(sm4Key, BaseData.TEST_ACCOUNT_NUMBER_2)); //工行联名卡号
        bizContent.setSecretKey(SecretUtils.encryptSM2(sm4Key)); //sm4对称密钥
        request.setBizContent(bizContent);
        SettlementAccountBindingQueryResponseV1 response = client.execute(request);
        System.out.println(JSON.toJSONString(response));
        if (response.isSuccess()) {
            for (String detail : response.getRecordDetail()) {
                TestObj testObj = JSON.parseObject(detail, TestObj.class);
                System.out.println("绑定卡号：" + SecretUtils.decryptMediumId(testObj.getBindMedium(), response.getSecretKey()));
            }
        } else {
        }

    }


    /**
     * 辅助测试类，用于打印绑定的卡号
     */
    static class TestObj {
        @JSONField(
                name = "bind_medium"
        )
        private String bindMedium;

        public String getBindMedium() {
            return bindMedium;
        }

        public void setBindMedium(String bindMedium) {
            this.bindMedium = bindMedium;
        }
    }
}
